<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME1;
  $password = PASSWORD1;
  $dbname = DATABASE_NAME1;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $username2 = USER_NAME2;
  $password2 = PASSWORD2;
  $dbname2 = DATABASE_NAME2;
  $myPdo2 = new PDO ('mysql:host=' . $hostname, $username2, $password2);
  $myPdo2->exec('USE ' . $dbname2);
  $myPdo2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}

try {
  $stmt = $myPdo->prepare("SELECT c.e_oid AS id, o.trackId as trackId
                           FROM ns_Customer AS c LEFT JOIN ns__ez_relation_customer_myorder AS mc ON c.e_oid = mc.oid_a 
                                                 LEFT JOIN ns_MyOrder AS o ON o.e_oid = mc.oid_b
                          ");
  $stmt->execute();
  
  $i = 0; 
  $res = array();
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res[] = $tmp;
    $i++;
  }
  
  
  $i = 0;
  $cond = '';
  for ( $i=0; $i<count($res);$i++ ) {
    $r = $res[$i];
    $cond .= "(orderTrackId != '$r->trackId' )";
    if($i<count($res)-1){
     $cond .= ' 
     AND ';
     }
  }
  
  
  $stmp = $myPdo2->prepare("SELECT p.e_oid, p.orderTrackId as orderTrackId FROM ns_Produce as p WHERE p.branch = 'NYC' AND $cond");
  $stmp->execute();
  
$j = 0; $res2 = array();
  while ($j < $stmp->rowCount()) {
    $tmp2 = $stmp->fetch(PDO::FETCH_OBJ);
    $res2[] = $tmp2;
    $j++;
  }
  
  echo json_encode(count($res2));
  MiscUtils::dump($res2);
} catch (PDOException $e) {
  echo $e->getMessage();
}
?>